package com.lishu.carsafe.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import com.lishu.carsafe.R;
import com.lishu.carsafe.constant.UserPreferences;
import com.lishu.carsafe.task.Upgrade;
import com.lishu.carsafe.utils.ToastUtil;
import com.lishu.carsafe.widget.DialogUtil;

public class MainActivity extends BaseActivity implements View.OnClickListener {
    private long exitTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        checkPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
    }

    private void initView() {
        setTopTitle(R.string.app_name);
        setTopLeft(R.drawable.icon_logout);
        //setTopLeftGone();
        findViewById(R.id.title_layout).setBackgroundColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogUtil.showLogoutDialog(mContext, new DialogUtil.OnOkButtonClickListener() {
                    @Override
                    public void onOkButtonClick() {
                        logout();
                    }
                });
            }
        });
    }

    protected void onRequestPermissionsGranted(int requestCode) {
        if (requestCode == 100) {
            try {
                new Upgrade(this, false).updateVersion();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_schedule_layout:
                startActivity(new Intent(this, ScheduleActivity.class));
                break;
            case R.id.task_layout:
                startActivity(new Intent(this, TaskActivity.class));
                break;
            case R.id.monitor_layout:
                startActivity(new Intent(this, MonitorActivity.class));
                break;
            case R.id.fault_layout:
                startActivity(new Intent(this, FaultActivity.class));
                break;
            case R.id.contact_layout:
                startActivity(new Intent(this, ContactListActivity.class));
                break;
            case R.id.statistics_layout:
                startActivity(new Intent(this, StatisticsActivity.class));
                break;
            default:
                break;
        }
    }

    private void logout(){
        UserPreferences.getInstance().logout();
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                ToastUtil.showShort("再按一次退出");
                exitTime = System.currentTimeMillis();
            } else {
                logout();
            }
            return false;
        }

        return super.onKeyDown(keyCode, event);
    }
}
